<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="机构" prop="departmentId">
        <el-select
          v-model="queryParams.departmentId"
          placeholder="请选择机构"
          filterable
          clearable
          @change="deptChange"
        >
          <el-option
            v-for="item in departmentList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="组织" prop="organizationId">
        <el-select
          v-model="queryParams.organizationId"
          placeholder="请选择组织"
          filterable
          clearable
          @change="orgChange"
        >
          <el-option
            v-for="item in orangeList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="停车场" prop="parkId">
          <el-select v-model="queryParams.parkId" placeholder="请选择" clearable >
            <el-option
              v-for="item in allParkList"
              :key="item.id"
              :label="item.name"
              :value="item.id"/>
          </el-select>
        </el-form-item>
      <el-form-item label="车牌号码" prop="plateNumber">
        <el-input
          v-model="queryParams.plateNumber"
          placeholder="请输入车牌号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="泊位编码" prop="berthCode">
        <el-input
          v-model="queryParams.berthCode"
          placeholder="请输入泊位编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phoneNumber">
        <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="欠费类型" prop="debtType">
        <el-select v-model="queryParams.debtType" placeholder="请选择欠费记录类型" clearable>
          <el-option
            v-for="item in dict.type.ECarcomeDebtType"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="出场时间" prop="dateRange">
        <el-date-picker
          v-model="queryParams.dateRange"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          :clearable="false"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          :default-time="['00:00:00','00:00:00']"
        ></el-date-picker>
        <!-- //23:59:59 -->
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="flex_hcneter">
      <div class="biankuan flex_hcneter borderLeft_1">
        <div class="contont">
          <p class="num txt-ellipsis">{{ total }}</p>
          <p class="title">账单记录（次）</p>
        </div>
      </div>

      <div class="biankuan flex_hcneter borderLeft_2">
        <div class="contont">
          <p class="num txt-ellipsis">{{ totalDebtAmount }}</p>
          <p class="title">账单欠费总金额（元）</p>
        </div>
      </div>
    </div>

    <el-table v-loading="loading" :data="spparkplaceList" >
      <el-table-column
      v-for="(item,index) in tableList"
      :key="index"
      :label="item.label" align="center" :prop="item.prop" :width="item.width ? item.width : ''">
        <template
        slot-scope="{row}">
          <dict-tag v-if="item.prop == 'type'" :options="dict.type.ECarcomeDebtType" :value="row[item.prop]"/>
          <div v-else>{{row[item.prop]}}</div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import {fetchList} from "@/api/park/arrearsRecord";
import { getDepartmentList,getListAuthPark } from '@/api/base'
import { listOrgs } from '@/api/system/organization'
import { YyMmDdToTimes,timesToYyMmDd1,getEndStartTime,getDay } from "@/utils/time.js";
export default {
  name: "arrearsRecord",
  dicts: ['ECarcomeDebtType',],
  components:{},
  data() {
    return {
      tableList:[
        {label:'机构名称',prop:'departmentName',},
        {label:'片区名称',prop:'parkName',},
        {label:'泊位编码',prop:'berthCode',},
        {label:'订单号',prop:'orderId',},
        {label:'车牌号码',prop:'plateNumber',},
        {label:'电话号码',prop:'phoneNumber',},
        {label:'停车时长',prop:'durationStr',},
        {label:'入场时间',prop:'enterTime',},
        {label:'出场时间',prop:'outTime',},
        {label:'欠费类型',prop:'type',},
        {label:'已缴金额',prop:'paidDebtAmount',},
        {label:'欠费金额',prop:'debtAmount',},
      ],//表格字段显示
      //片区数据
      areaList: [],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      totalDebtAmount: 0,//欠费总金额
      // 车位信息表格数据
      spparkplaceList: [],
      // 查询参数
      queryParams: {
        page: 1,
        pageSize: 10,
        dateRange:[],
      },
      departmentList: [],//机构数据
      allParkList: [],//停车场数据
      orangeList:[],//组织数据
      shijiancha:0,
    };
  },
  created() {
    this.getDepartmentList();
    this.getList();
  },
  methods: {
    getSearchTime(){//获取搜索时间
      let startTime = this.queryParams.dateRange[0]
      let endTime = this.queryParams.dateRange[1]
      let startTimeStr = YyMmDdToTimes(startTime)
      let endTimeStr = YyMmDdToTimes(endTime)
      let tian90 = this.shijiancha
      // console.log('startTimeStr',startTimeStr)
      // console.log('endTimeStr',endTimeStr)
      // console.log('zendTimeStr',startTimeStr + tian90)
      return startTimeStr + tian90 < endTimeStr
    },
    getNowTime(){//初始筛选时间
      var today1 = new Date() //当天
      let mingtian = getDay(1)
      var today = new Date(mingtian) //当天
      today.setMonth(today.getMonth()-3)//三个月前，时间戳
      today.toLocaleDateString()//格式化时间
      let starTimes = timesToYyMmDd1(today,'YY-MM-DD')
      let starTimesStr = getEndStartTime([starTimes,starTimes])
      // let endTimes = timesToYyMmDd1(today1,'YY-MM-DD')
      let endTimesStr = getEndStartTime([mingtian,mingtian])
      let startStr = YyMmDdToTimes(starTimesStr.startTime)
      let endStr = YyMmDdToTimes(endTimesStr.startTime)
      this.shijiancha = endStr - startStr
      this.queryParams.dateRange = [starTimesStr.startTime,endTimesStr.startTime]
    },
    async listOrgs(departmentId){//根据机构获取组织
      let res = await listOrgs({departmentId})
    	let list = res.data || []
    	this.orangeList = [...list]
    },
    async deptChange(e){//机构下拉选择
      let queryParams = {...this.queryParams}
      queryParams.parkId = ''
      queryParams.organizationId = ''
      this.queryParams = {...queryParams}
      this.allParkList = []
      this.orangeList = []
      if(!e) return
      await this.listOrgs(e)
    },
    async orgChange(e){//组织下拉选择
      let queryParams = {...this.queryParams}
      queryParams.parkId = ''
      this.queryParams = {...queryParams}
      this.allParkList = []
      if(!e) return
      this.allParkList = await this.getListAuthPark(e)
    },
    getDepartmentList(){//获取机构
      getDepartmentList().then(res => {
        this.departmentList = res.data || []
      })
    },
    async getListAuthPark(organizationId){//获取停车场
      let res = await getListAuthPark({organizationId,parkType:2})
      return res.data || []
    },
    /** 查询车位信息列表 */
    async getList() {

      if(!this.queryParams.dateRange) this.queryParams.dateRange = []
      if(!this.queryParams.dateRange.length) this.getNowTime()
      let queryParams = {
        ...this.queryParams
      }
      let flag = await this.getSearchTime()
      if(flag) return this.$modal.msgWarning('请选择时间范围为三个月的数据');
      this.loading = true;
      if(queryParams.dateRange.length) {
        queryParams.startTime = queryParams.dateRange[0]
        queryParams.endTime = queryParams.dateRange[1]
      }
      delete queryParams.dateRange
      delete queryParams.date
      fetchList(queryParams).then(response => {
        let data = response.data || {}
        let rows = data.list || []
        let obj = rows[0] || {}
        this.totalDebtAmount = obj.totalDebtAmount || 0
        this.spparkplaceList = rows
        this.total = data.total || 0;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.dateRange = []
      this.handleQuery();
    },
  }
};
</script>
<style lang="scss">
 .amap-icon img,.amap-icon,
.amap-marker-content img{
    width: 25px!important;
    height: 34px!important;
}
#container {width:650px; height: 300px; }
</style>
